package com.gitee.cashzhang27.test.boot.redisson.controller;

import com.gitee.cashzhang27.test.boot.redisson.common.enmu.ServiceCodeEnums;
import com.gitee.cashzhang27.test.boot.redisson.helper.ServiceCodeHelper;
import java.util.stream.IntStream;
import lombok.AllArgsConstructor;
import org.redisson.api.RLock;
import org.redisson.api.RLongAdder;
import org.redisson.api.RedissonClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Cash Zhang
 * @version v1.0
 * @since 2019/10/20 01:38
 */
@RestController
@AllArgsConstructor
@RequestMapping("test")
public class ServiceCodeController {


  private RedissonClient redissonClient;
  private ServiceCodeHelper serviceCodeHelper;

  @GetMapping("1")
  public String test1() {
    String serviceCode = serviceCodeHelper.fetchServiceCode(ServiceCodeEnums.USER_CODE);
    System.out.println(serviceCode);
    return serviceCode;
  }

  @GetMapping("2")
  public long test2() {
    RLock myLock = redissonClient.getLock("myLock");
    myLock.tryLock();
    try {
      RLongAdder atomicLong = redissonClient.getLongAdder("myLongAdder");
      IntStream.range(0, 9).forEach(i -> {
        atomicLong.increment();
        long sum = atomicLong.sum();
        System.out.println(sum);
      });
      return 0L;
    } finally {
      myLock.unlock();
    }
  }

}
